﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace KD.Sms
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void benCanel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string _strUser = this.txtUser.Text.Trim();
            string _strPassword = this.txtPwd.Text;
            if (string.IsNullOrEmpty(_strUser))
            {
                UI.MsgBox.Show(UI.MsgType.Waring, "请输入帐号");
                this.txtUser.SelectAll();
                this.txtUser.Focus();
            }
            else if (string.IsNullOrEmpty(_strPassword))
            {
                UI.MsgBox.Show(UI.MsgType.Waring, "请输入密码");
                this.txtPwd.SelectAll();
                this.txtPwd.Focus();
            }
            else
            {
                this.btnLogin.Enabled = false;
                DataTable _tabUser = DAL.SqlLite.Main.Instance.DoLogin(_strUser, DesEncrypt.Encrypt(_strPassword));
                if (_tabUser != null && _tabUser.Rows.Count == 1)
                {
                    if (_strUser == "admin")
                    {
                        CacheUser.E_IsAdmin = true;
                        CacheUser.E_KeyInformation = true;//管理员显示全部
                        CacheUser.E_LockSampling = int.Parse(_tabUser.Rows[0]["User_State"].ToString()) == 1;
                        CacheUser.User_Id = int.Parse(_tabUser.Rows[0]["User_id"].ToString());
                        CacheUser.User_Acct = _tabUser.Rows[0]["User_Acct"].ToString();
                        CacheUser.User_PassWord = DesEncrypt.Decrypt(_tabUser.Rows[0]["User_Pwd"].ToString());
                        CacheUser.Chinese_name = _tabUser.Rows[0]["Chinese_Name"].ToString();
                        DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, "成功登录系统");
                        base.DialogResult = DialogResult.OK;
                        base.Close();
                    }
                    else
                    {
                        CacheUser.E_IsAdmin = false;
                        CacheUser.E_LockSampling = false;
                        if (int.Parse(_tabUser.Rows[0]["User_State"].ToString()) == 1)
                        {
                            CacheUser.E_KeyInformation = _tabUser.Rows[0]["User_KeyInformation"].ToIntEx() == 1;
                            CacheUser.User_Id = int.Parse(_tabUser.Rows[0]["User_id"].ToString());
                            CacheUser.User_Acct = _tabUser.Rows[0]["User_Acct"].ToString();
                            CacheUser.User_PassWord = DesEncrypt.Decrypt(_tabUser.Rows[0]["User_Pwd"].ToString());
                            CacheUser.Chinese_name = _tabUser.Rows[0]["Chinese_Name"].ToString();
                            DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, "成功登录系统");
                            base.DialogResult = DialogResult.OK;
                            base.Close();
                        }
                        else
                        {
                            UI.MsgBox.Show(UI.MsgType.Error, "登录系统失败。当前账户已被禁用。");
                        }
                    }
                }
                else
                {
                    UI.MsgBox.Show(UI.MsgType.Error, "登录系统失败。帐号或密码不正确。");
                    this.txtUser.SelectAll();
                    this.txtUser.Focus();
                    this.txtPwd.Text = "";
                    this.btnLogin.Enabled = true;
                }
            }
        }

        private void txtPwd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Control || e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }

        }
    }
}
